package com.meida.module.bus.provider.service.impl;

import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.utils.ApiAssert;
import com.meida.common.utils.DateUtils;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.admin.client.entity.BaseUser;
import com.meida.module.bus.client.entity.BusMaterialPlan200;
import com.meida.module.bus.client.entity.BusMaterialView;
import com.meida.module.bus.client.entity.BusProject;
import com.meida.module.bus.client.entity.BusVersion;
import com.meida.module.bus.provider.mapper.BusVersionMapper;
import com.meida.module.bus.provider.service.BusVersionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 版本记录接口实现类
 *
 * @author flyme
 * @date 2020-10-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusVersionServiceImpl extends BaseServiceImpl<BusVersionMapper, BusVersion> implements BusVersionService {


    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusVersion version, EntityMap extra) {
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusVersion> cq, BusVersion version, EntityMap requestMap) {
        cq.select("version.*");
        cq.select("user.userName,project.projectName,project.projectCode");
        if (FlymeUtils.isNotEmpty(requestMap.get("versionCode"))) {
            cq.createJoin(BusMaterialPlan200.class).setMainField("versionId").setJoinField("versionId");
        } else {
            cq.createJoin(BusMaterialView.class).setMainField("versionId").setJoinField("versionId");
            cq.eq("bmv.projectId", requestMap.get("projectId"));
            cq.eq("bmv.type", requestMap.get("type"));
        }
        cq.createJoin(BaseUser.class).setMainField("userId").setJoinField("userId");
        cq.createJoin(BusProject.class).setMainField("projectId").setJoinField("projectId");
        cq.eq("version.projectId", requestMap.get("projectId"));
        cq.groupBy("version.versionId");
        cq.orderByDesc("version.createTime");
        return ResultBody.ok();
    }

    @Override
    public EntityMap selectLatestVersion(Long projectId,Integer versionType) {
        CriteriaQuery cq = new CriteriaQuery(BusVersion.class);
        cq.eq("projectId", projectId);
        cq.eq("versionType", versionType);
        cq.orderByDesc("createTime");
        cq.last("LIMIT 1");
        return findOne(cq);
    }

    @Override
    public BusVersion getLatestVersion(Long projectId,Integer versionType) {
        CriteriaQuery cq = new CriteriaQuery(BusVersion.class);
        cq.eq("projectId", projectId);
        cq.eq("versionType", versionType);
        cq.orderByDesc("createTime");
        cq.last("LIMIT 1");
        return getOne(cq);
    }

    @Override
    public List<BusVersion> selectList(Long projectId) {
        CriteriaQuery cq = new CriteriaQuery(BusVersion.class);
        cq.eq("projectId", projectId);
        cq.orderByDesc("createTime");
        return list(cq);
    }

    @Override
    public BusVersion getNewBusVersion(Long projectId,Integer versionType, Integer type) {
        BusVersion latestVersion = getLatestVersion(projectId,versionType);
        ApiAssert.isNotEmpty("版本信息不存在", latestVersion);
        BusVersion newBusVersion = new BusVersion();
        BeanUtils.copyProperties(latestVersion, newBusVersion);
        if (null == type || type == 1) {
            String smallVerion = newBusVersion.getSmallVerion();
            Integer small = TypeUtils.castToInt(smallVerion) + 1;
            if (small < 10) {
                smallVerion = "0" + small;
            } else {
                smallVerion = TypeUtils.castToString(small);
            }
            newBusVersion.setSmallVerion(smallVerion);
        } else if (type == 2) {
            String bigVersion = newBusVersion.getBigVersion();
            Integer big = TypeUtils.castToInt(bigVersion) + 1;
            if (big < 10) {
                bigVersion = "0" + big;
            } else {
                bigVersion = TypeUtils.castToString(big);
            }
            newBusVersion.setBigVersion(bigVersion);
        }
        newBusVersion.setVersionId(IdWorker.getId());
        newBusVersion.setVersionType(versionType);
        newBusVersion.setUpdateTime(null);
        newBusVersion.setCreateTime(DateUtils.getNowDateTime());
        return newBusVersion;
    }

}
